package org.darkforest.common.services.impl;

import org.darkforest.common.services.GeneralService;
import org.darkforest.common.services.WxJSSdkService;
import org.darkforest.common.util.SmsSendUtil;
import org.darkforest.dao.dao.GeneralDao;
import org.darkforest.enums.TempleEnum;
import org.darkforest.listener.SpringContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by huayunsky on 11/9/2015.
 */

@Service
public  class GeneralServiceImpl<T, E> extends BaseServiceImpl<T, E> implements GeneralService<T> {

    private GeneralDao<T, E> generalDao;

    @Autowired
    private WxJSSdkService wxJSSdkService;

    @Override
    public GeneralDao<T, E> getDao() {
        return generalDao;
    }
    public void setGeneralDao(GeneralDao<T, E> generalDao) {
         this.generalDao = generalDao;
    }

    @Override
    public List<T> selectByCondition(T object) {
        generalDao = (GeneralDao) SpringContextHolder.getContextHolder().getContext().getBean(object.getClass().getSimpleName().toLowerCase()+"DaoImpl");
        return getDao().selectByCondition(object);
    }

    public void sender(String[] params,String tempId ,String to,int type){
        if(type==0){
            SmsSendUtil.newInstance().send(to,TempleEnum.getBykey(tempId).getKey(), params);
        }else if(type==1){
            SendWechatMsg(params, tempId, to);
        }else if (type==2){
            SmsSendUtil.newInstance().send(to, SmsSendUtil.SMS_TEMPLETE_CAPTCHA_NO, params);
            SendWechatMsg(params, tempId, to);
        }
    }
    public void SendWechatMsg(String[] params, String tempId, String to) {
        String content = TempleEnum.getBykey(tempId).getValue();
        for (int i=0;i<params.length;i++){
             content = content.replace("{"+(i+1)+"}",params[i]);
        }
        wxJSSdkService.sendWechatMessage(to,content);
    }

}
